package com.mydiceplayer.adapter;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mydiceplayer.R;
import com.mydiceplayer.models.ModelVideo;
import com.mydiceplayer.utils.UtilUi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.provider.MediaStore.Video;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterVideo extends BaseAdapter {

	List<ModelVideo> videos;
	Context context;
	
	Thumbnails thumbnails = new Thumbnails();
	
//	SoftReference<Map<Integer, Bitmap>> cacheThumbnail;
	
	Map<Integer, SoftReference<Bitmap>> cacheThumbnail = new HashMap<Integer, SoftReference<Bitmap>>();
	
	
	/**
	 * @param videos
	 * @param context
	 */
	public AdapterVideo(List<ModelVideo> videos, Context context) {
		super();
		this.videos = videos;
		this.context = context;
	}

	@Override
	public int getCount() {
		return videos.size();
	}

	@Override
	public Object getItem(int position) {
		return videos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = UtilUi.layoutInflater(context, R.layout.item_videos);
			viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
			viewHolder.len = (TextView) convertView.findViewById(R.id.len);
			
			viewHolder.videoName = (TextView) convertView.findViewById(R.id.videoName);
			viewHolder.videoSize = (TextView) convertView.findViewById(R.id.videoSize);
			viewHolder.playPosition = (TextView) convertView.findViewById(R.id.playPosition);
			viewHolder.totalLen = (TextView) convertView.findViewById(R.id.totalLen);
		
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		
		ModelVideo video = videos.get(position);
		
		
		Bitmap miniThumb = getVideoThumbnail(video.getId());
		viewHolder.thumbnail.setImageBitmap(miniThumb);;
		viewHolder.len.setText(video.getCalculatedDuration());
		viewHolder.videoName.setText(video.getDisplayName());
		viewHolder.videoSize.setText(video.getCalculatedSize());
		viewHolder.playPosition.setText(video.getCalculatedPlayPosition());
		viewHolder.totalLen.setText(video.getCalculatedDuration());
		return convertView;
	}

	private class ViewHolder{
		ImageView thumbnail;
		TextView len;
		
		
		TextView videoName;
		TextView videoSize;
		TextView playPosition;
		TextView totalLen;
	}

	public List<ModelVideo> getVideos() {
		return videos;
	}

	public void setVideos(List<ModelVideo> videos) {
		this.videos = videos;
	}
	
	private Bitmap getVideoThumbnail(int videoId){
		Bitmap bitmap;
		SoftReference<Bitmap> reference = cacheThumbnail.get(videoId);
		if(reference != null){
			bitmap = reference.get();
			if(bitmap != null){
				return bitmap;
			}
		}
		
		bitmap = Video.Thumbnails.getThumbnail(context.getContentResolver(), videoId, Thumbnails.MICRO_KIND, null);
	    if(bitmap == null){
	    	bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ready);
	    }
		
		reference = new SoftReference<Bitmap>(bitmap);  
	    cacheThumbnail.put(videoId, reference);  
		return bitmap;
	}
}
